<?php

namespace App\Admin\Controllers\Customer;

use App\Admin\Actions\CustomerImport;
use App\Admin\Actions\Post\ImportPost;
use App\Events\Customer\CustomerSaved;
use App\model\customer\Customer;
use App\model\customer\CustomerArea;
use App\model\customer\CustomerBigDepart;
use App\model\customer\CustomerHangye;
use App\model\customer\CustomerHuanping;
use App\model\customer\CustomerPeidian;
use App\model\customer\CustomerSource;
use App\model\Customer\CustomerWuye;
use App\model\Customer\CustomerXiaofang;
use App\model\Customer\CustomerXuqiu;
use App\Notifications\WechatNotification;
use App\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;

class CustomerController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '客户管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Customer());


        $grid->fixColumns(2, -3);
        $grid->column('customer_id', __('id'));
        $grid->column('code', __('code'));
        $grid->column('name', __('姓名'));
        $grid->column('telephone', __('电话'))->expand(function($model){
            $mobile=$model->mobiles()->get()->map(function($mobile){
                    return $mobile->only(['id','mobile']);
            });
            return new Table(['id','手机号'],$mobile->toArray());

        });
        $grid->column('address', __('位置'));
        $grid->column('wuye_format', __('物业'));
        $grid->column('hangye_format', __('行业'));
        $grid->column('area_format', __('面积'));
        $grid->column('peidian_format', __('配电'));
        $grid->column('xuqiu_format', __('需求'));
        $grid->column('source_format', __('来源'));
        $grid->column('xiaofang_format', __('消防'));
        $grid->column('huanping_format', __('环评'));
        $grid->column('desc', __('描述'));
        $grid->column('status', __('状态'))->display(function ($val){
        return Customer::$statusMap[$val];
        });
        $grid->column('follow_status', __('跟进状态'))->display(function ($val){
            if ($this->status==Customer::FOLLOW_TO_USER){
                return Customer::$followMap[$val] ?? '';
            }
           return '';
        });
        $grid->column('ower_user_id', __('负责人'))->display(function ($val){
            return User::find($val)->name ?? "";
        });
        $grid->column('create_user_id', __('创建者'))->display(function ($val){
            return $this->create_user_name  ?? (User::find($val)->name ?? ''  );
        });

        $grid->tools(function (Grid\Tools $tools) {
            $tools->append(new ImportPost());
            $tools->append(new CustomerImport());
        });
        //$grid->column('deleted_at', __('Deleted at'));
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('更新时间'));
        //筛选功能
        $grid->filter(function (Grid\Filter $filter) {
            // 去掉默认的id过滤器
            $filter->column(1 / 2, function (Grid\Filter $filter) {
                $type = [
                    ' ' => '全部',
                    0 => '出租',
                    1 => '出售',
                    2 => '可租可售',
                ];
                $filter->equal('code','code');
                $filter->equal('status','状态')->select(Customer::$statusMap);
                $filter->equal('status','跟进状态')->select(Customer::$followMap);
                $filter->disableIdFilter();
                $filter ->where(function($q){
                        $q->whereHas('mobiles',function($query){
                            $query->where('mobile',$this->input);
                        });
                },'手机号');
                $filter->equal('wuye_id', '物业')->select(CustomerWuye::all()->pluck('name', 'wuye_id'));
                $filter->equal('hangye_id', '行业')->select(CustomerHangye::all()->pluck('name', 'hangye_id'));
                $filter->equal('area_id', '面积')->select(CustomerArea::all()->pluck('name', 'area_id'));
                $filter->equal('xuqiu_id', '需求')->select(CustomerXuqiu::all()->pluck('name', 'xuqiu_id'));
            });

            $filter->column(1 / 2, function ($filter) {
                $filter->equal('peidian_id', '配电')->select(CustomerPeidian::all()->pluck('name', 'peidian_id'));
                $filter->equal('xiaofang_id', '消防')->select(CustomerXiaofang::all()->pluck('name', 'xiaofang_id'));
                $filter->equal('huanping_id', '环评')->select(CustomerHuanping::all()->pluck('name', 'huanping_id'));
                $filter->equal('source_id', '来源')->select(CustomerSource::all()->pluck('name', 'source_id'));
            });
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Customer::findOrFail($id));
        $show->field('customer_id', __('Customer id'));
        $show->field('address', __('Address'));
        $show->field('name', __('Name'));
        $show->field('area_id', __('Area id'));
        $show->field('hangye_id', __('Hangye id'));
        $show->field('peidian_id', __('Peidian id'));
        $show->field('xuqiu', __('Xuqiu'));
        $show->field('soure_id', __('Soure id'));
        $show->field('ower_user_id', __('Ower user id'));
        $show->field('create_user_id', __('Create user id'));
        $show->field('deleted_at', __('Deleted at'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('desc', __('Desc'));
        $show->field('huanping_id', __('Huanping id'));
        $show->field('status', __('Status'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Customer());

        $form->column(1/2, function ($form) {
            $form->text('name', __('姓名'));
            $form->text('address', __('位置'));
            $form->select('ower_user_id', __('负责人'))->options(User::all()->pluck('name', 'id'));
            $form->hasMany('mobiles','手机号', function (Form\NestedForm $form) {
                $form->text('mobile','手机号');

            });
            $form->textarea('desc', __('备注信息'));
        });
        $form->column(1/2, function ($form) {
            $form->select('wuye_id', __('物业类型'))->options(CustomerWuye::all()->pluck('name', 'wuye_id'))->default([]);
            $form->select('hangye_id', __('行业'))->options(CustomerHangye::all()->pluck('name', 'hangye_id'));
            $form->select('area_id', __('面积'))->options(CustomerArea::all()->pluck('name', 'area_id'));
            $form->select('xuqiu_id', __('需求'))->options(CustomerXuqiu::all()->pluck('name', 'xuqiu_id'));
            $form->select('peidian_id', __('配电'))->options(CustomerPeidian::all()->pluck('name', 'peidian_id'));
            $form->select('xiaofang_id', __('消防'))->options(CustomerXiaofang::all()->pluck('name', 'xiaofang_id'));
            $form->select('huanping_id', __('环评'))->options(CustomerHuanping::all()->pluck('name', 'huanping_id'));
            $form->select('source_id', __('来源'))->options(CustomerSource::all()->pluck('name', 'source_id'));
            $form->select('big_part_id', __('所属大组'))->options(CustomerBigDepart::all()->pluck('name', 'id'));
            $form->select('create_user_id', __('创建人'))->options(User::all()->pluck('name', 'id'));
        });
        $form->saved(function (Form $form){
            //推送
           if( $form->isCreating()){

               $users = User::permission('allot_group')
                   ->get();
               if ($users->isNotEmpty()){
                   foreach ($users as $item){
                       $item->notify(new WechatNotification($form->model()));
                   }
               }
           }
            $customer_id =$form->model()->customer_id;
            event(new CustomerSaved(Customer::find($customer_id)));
        });
        return $form;
    }
}
